package fonctions;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

import interfaces.Saveable;
import ensemble.Echantillon;
import exceptions.OperationMatriceException;
import structure.Caracteristique;
import structure.Couche;

public class Reconnaissance implements Saveable{
	public Caracteristique c;
	public Couche[] couches;

	public Reconnaissance(Caracteristique reseau) {
		couches= new Couche[reseau.layers];
		couches[0]= new Couche(reseau.neurones[0], reseau.entree, 2);
		for (int i=1; i<reseau.layers; i++) {
			couches[i]= new Couche(reseau.neurones[i], reseau.neurones[i-1], 1);
		}
	}

	public int reconnaissance(Echantillon e) {
		// Activation couche 1
		//Operation op= new Operation();
		try {
			couches[0].somme= couches[0].poids.produit(e.getSequence()).somme(couches[0].biais);
		}catch (OperationMatriceException pb) {
			System.out.println(pb);
		}
		switch (couches[0].fonction) {
		case 1:
			couches[0].activat= couches[0].somme;
			break;
		case 2:
			couches[0].activat= Operation.logSigmoid(couches[0].somme);
			break;
		default:
			System.out.println("Unknown fonction");
			System.exit(0);
			break;
		}

		// Activation couche [2, M]
		if (couches.length>=2) {
			for (int i=1; i<couches.length; i++) {
				try{
					couches[i].somme= couches[i].poids.produit(couches[i-1].activat).somme(couches[i].biais);
				}catch (OperationMatriceException ex) {
					System.out.println(ex);
				}
				switch (couches[i].fonction) {
				case 1:
					couches[i].activat= couches[i].somme;
					break;
				case 2:
					couches[i].activat= Operation.logSigmoid(couches[i].somme);
					break;
				default:
					System.out.println("Unknown fonction");
					System.exit(0);
					break;
				}
			}
		}
		int res= Operation.max(couches[couches.length-1].activat);
		return res;
	}

	public void save () {
		try {
			savePoids();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			saveBiais();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void savePoids() throws IOException{
		FileWriter fw=null;
		PrintWriter print=null;
		try {
			fw= new FileWriter("./networkDataPoids.txt");
			print= new PrintWriter(fw);
		}catch (IOException io) {
			System.out.println(io);
		}
		for (int i=0; i<couches.length; i++) {
			print.print(couches[i].poids);
			print.println("----------------------");
		}
		fw.close();
	}

	public void saveBiais() throws IOException{
		FileWriter fw=null;
		PrintWriter print=null;
		try {
			fw= new FileWriter("./networkDataBiais.txt");
			print= new PrintWriter(fw);
		}catch (IOException io) {
			System.out.println(io);
		}
		for (int i=0; i<couches.length; i++) {
			print.print(couches[i].biais);
			print.println("----------------------");
		}
		fw.close();
	}

	/*
	public void load () throws IOException {
		FileReader fr = null;
		Scanner sc = null;
		try {
			fr= new FileReader("./networkDataPoids.txt");
			sc= new Scanner(fr);
		}catch (IOException io) {
			System.out.println(io);
		}
		sc.useDelimiter(arg0)
	}
	*/
}
